package com.training.android.emailform;

import java.util.regex.Pattern;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.inputmethod.InputMethodManager;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class EmailFormActivity extends Activity {
	/** Called when the activity is first created. */

	EditText emailEditText, passwordEditText;
	Button signinButton;

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		initComponents();
	}

	private void initComponents() {
		emailEditText = (EditText) findViewById(R.id.emailEditText);
		passwordEditText = (EditText) findViewById(R.id.passwordEditText);
		signinButton = (Button) findViewById(R.id.signinButton);
		signinButton.setOnClickListener(new View.OnClickListener() {
			public void onClick(View v) {
				signinButtonClicked(v);
			}
		});
	}

	private void signinButtonClicked(View v) {
		InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE);
		imm.hideSoftInputFromWindow(emailEditText.getWindowToken(), 0);
		imm.hideSoftInputFromWindow(passwordEditText.getWindowToken(), 0);

		String email = emailEditText.getText().toString();
		String password = passwordEditText.getText().toString();
		if (email.length() <= 0 || password.length() <= 0) {
			Toast.makeText(getApplicationContext(), "please fill the details",
					Toast.LENGTH_SHORT).show();
			return;
		}
		if (EMAIL_ADDRESS_PATTERN.matcher(email).matches()) {
			if (password.length() < 5) {
				Toast.makeText(getApplicationContext(),
						"password too short , minimum 5 characters",
						Toast.LENGTH_SHORT).show();
				return;
			} else {
				Toast.makeText(getApplicationContext(), "you are signed in",
						Toast.LENGTH_SHORT).show();
				return;
			}
		} else {
			Toast.makeText(getApplicationContext(), "enter email properly",
					Toast.LENGTH_SHORT).show();
			return;
		}
	}

	public final Pattern EMAIL_ADDRESS_PATTERN = Pattern
			.compile("[a-zA-Z0-9\\+\\.\\_\\%\\-\\+]{1,256}" + "\\@"
					+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,64}" + "(" + "\\."
					+ "[a-zA-Z0-9][a-zA-Z0-9\\-]{0,25}" + ")+");

}